% --------------------------------------------------------
% Copyright (c) Weiyang Liu, Yandong Wen
% Licensed under The MIT License [see LICENSE for details]
%
% Intro:
% This script is used to evaluate the performance of the trained model on LFW dataset.
% We perform 10-fold cross validation, using cosine similarity as metric.
% More details about the testing protocol can be found at http://vis-www.cs.umass.edu/lfw/#views.
% 
% Usage:
% cd $SPHEREFACE_ROOT/test
% run code/evaluation.m
% --------------------------------------------------------

function evaluation()

clear;clc;close all;
cd('../')

%% caffe setttings
matCaffe = fullfile(pwd, '../tools/caffe-sphereface/matlab');
addpath(genpath(matCaffe));
gpu = 1;
if gpu
   gpu_id = 0;
   caffe.set_mode_gpu();
   caffe.set_device(gpu_id);
else
   caffe.set_mode_cpu();
end
caffe.reset_all();

model   = '../train/code/mytestdata_deploy_score.prototxt';
%weights = '../train/result/sphereface_model_iter_28000.caffemodel';
weights = '../train/result/mytestdata_model_iter_28000.caffemodel';
net     = caffe.Net(model, weights, 'test');

[classFolders, files] = differentData(fullfile(pwd, 'data/my_testData-112X96'), 'my_testData-112X96', 0);
start_class_num = 1
test_class_num  = 200

correct_arr     = zeros([test_class_num,1]);

for i = start_class_num:start_class_num+test_class_num-1
    i_folder_name   = classFolders{i};
    i_files         = files{i};
    %disp(i_files);
    %disp(class(i_files))
    % loop on i_folder_name
    i_correct_num   = 0;
    i_num           = length(i_files);
    for j =1:length(i_files)
        j_file  = i_files{j};
        %disp(j_file)
        %disp(class(j_file))
        %fprintf('read im:%s', j_file)
        j_score = extractDeepScore(j_file, net);
        j_score = cell2mat(j_score);
        [j_max_score, j_index]  = max(j_score);
        if j_index == i
            i_correct_num   = i_correct_num + 1;
        end
    end
    i_acc   = i_correct_num / double(i_num);
    fprintf('%d-th class: %s, accuracy is: %f\n', i, i_folder_name, i_acc);
    correct_arr(i - start_class_num + 1, 1) = i_acc;
end
fprintf('average acc is: %f', mean(correct_arr));
fprintf('\n');
end

function score = extractDeepScore(file, net)
    img     = single(imread(file));
    img     = (img - 127.5)/128;
    img     = permute(img, [2,1,3]);
    img     = img(:,:,[3,2,1]);
    res     = net.forward({img});
    score   = res;
end

function [subFolders, files] = differentData(folder, name, ignore_num)
    if nargin < 3
      ignore_num = 0;
    end
    
    subFolders = struct2cell(dir(folder))';
    subFolders = subFolders(3:end, 1);
    files      = cell(size(subFolders));
    for i = 1:length(subFolders)
        fprintf('%s --- Collecting the %dth folder (total %d) ...\n', name, i, length(subFolders));
        subList  = struct2cell(dir(fullfile(folder, subFolders{i}, '*.jpg')))';
        %files{i} = fullfile(folder, subFolders{i}, subList(:, 1));
        files{i} = fullfile(folder, subFolders{i}, subList(1+ignore_num:end, 1));
    end
end

